layui.define(['form','treeTable', 'upload'], function(exports){
  var $ = layui.$,form = layui.form,treeTable = layui.treeTable,upload = layui.upload;

    treeTable.render({
        elem: '#LAY-table',
        tree: {
            iconIndex: 1,           // 折叠图标显示在第几列
            isPidData: true,        // 是否是id、pid形式数据
            idName: 'id',  			// id字段名称
            pidName: 'parentId'     // pid字段名称
        },
        cols: [[
            {field:'id', title: 'ID',width:80}
            ,{field:'name', title: '类型名称'}
            ,{field:'code', title: '类型Code'}
            ,{field:'priority', title: '优先级',width:80}
            ,{field:'updateDate',title: '修改时间',width:120}
            ,{field:'createDate',title: '创建时间',width:120}
            ,{title: '操作', width: 200, align:'center', toolbar: '#LAY-table-toolbar'}
        ]],
        reqData: function(data, callback) {
            var parentId = null;
            if(data != null){parentId = data.id;}
            $.get(layui.setter.webRoot + '/place/physical_store/type/list.data',{
                parentId:parentId
            },function (res) {
                if(res.code==200) callback(res.body.data);
                else callback(res.msg);
            });
        }
    });

    //监听搜索
    form.on('submit(LAY-search)', function(data){
        var field = data.field;
        //执行重载
        treeTable.reload('LAY-table', {
            where: field
            ,page: {curr:1}
        });
    });

    var active = {
        add: function(){
            layer.open({
                type: 2
                ,title: '新增网点类型'
                ,content: layui.setter.webRoot + '/place/physical_store/type/add.html'
                ,maxmin: false
                ,area: ['550px', '400px']
                ,btn: ['确定','取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + "/place/physical_store/type/add.do",
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    treeTable.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.click();
                }
            });
        }
        ,update:function(obj){
            layer.open({
                type: 2
                ,title: '修改网点类型'
                ,content: layui.setter.webRoot + '/place/physical_store/type/update.html?id='+obj.data.id
                ,maxmin: false
                ,area: ['550px', '400px']
                ,btn: ['确定','取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + "/place/physical_store/type/update.do",
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    treeTable.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,addChild:function(obj){
            layer.open({
                type: 2
                ,title: '新增下级网点类型'
                ,content: layui.setter.webRoot + '/place/physical_store/type/add.html?parentId='+obj.data.id
                ,maxmin: false
                ,area: ['550px', '400px']
                ,btn: ['确定','取消']
                ,yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submit = layero.find('iframe').contents().find("#LAY-form-submit");
                    iframeWindow.layui.form.on('submit(LAY-form-submit)', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + "/place/physical_store/type/add.do",
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    treeTable.reload('LAY-table'); //数据刷新
                                    layer.close(index); //关闭弹层
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.click();
                }
            });
        }
        ,del:function(obj){
            layer.confirm('您确定要删除该网点类型吗?(无法删除还在使用中的网点类型)', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + "/place/physical_store/type/del.do",
                    data:{id:obj.data.id},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
        ,exportExcel:function (){
            layer.confirm('确定要导出全部网点类型吗？', function(index){
                $('#exportAllForm').submit();
                layer.close(index);
            });
        }
        ,importExcel:function (){
            $('#uploadExcel').click();
        }
    };

    $('.layui-btn.LAY-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    treeTable.on('tool(LAY-table)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

    upload.render({
        elem: '#uploadExcel'
        , url: layui.setter.webRoot + '/place/physical_store/type/excel/import.do'
        , accept: 'file'
        , before: function (obj) {
            layer.load(); //上传loading
        }
        , done: function (res) {
            if (res.code == 200) {
                layer.msg("导入成功", {icon: 1});
                table.reload('LAY-table');
                layer.close(index);
            } else {
                layer.msg(res.msg, {icon: 5});
            }
            layer.closeAll('loading'); //关闭loading
        }
        , error: function (res) {
            //请求异常回调
            layer.closeAll('loading');
            layer.msg(res.msg, {icon: 5});
        }
    });

  exports('physical_store_type_list', {})
});